package com.csyf.manager.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.csyf.manager.entity.*;
import com.csyf.manager.service.OrderService;
import com.csyf.manager.service.ProxyQueryBiz;
import com.csyf.manager.service.SysTradeSummaryService;
import com.csyf.manager.service.SysTradeTypeBiz;
import com.csyf.manager.utils.page.JqGridVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping(value = "sys/order")
public class OrderController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private HttpSession httpSession;

    @Autowired
    private SysTradeTypeBiz sysTradeTypeBiz;

    @Autowired
    private SysTradeSummaryService sysTradeSummaryService;

    @Autowired
    private ProxyQueryBiz proxyQueryBiz;

    @GetMapping(value = "orderview")
    public String view(Model model) {
        SysMerchant userInfo = getUserInfo();

        List<SysTradeType> tradeTypeByMchId = sysTradeTypeBiz.findTradeTypeByMchId(userInfo.getMerchMerchantNo());
        List<SysTradeType> sysTradeTypes = new ArrayList<>();
        for (SysTradeType entry : tradeTypeByMchId) {
            if (!entry.getTradeType().contains("DF")) {
                sysTradeTypes.add(entry);
            }
        }
        model.addAttribute("tradeList", sysTradeTypes);
        return "view/orderpage";
    }


    @GetMapping(value = "billview")
    public String billview() {

        return "view/billpage";
    }


    @GetMapping(value = "balanceview")
    public String balanceview(Model mv) {

        SysMerchant userInfo = getUserInfo();

        List<BalanceShowVo> showVos = proxyQueryBiz.queryBalance(userInfo.getMerchMerchantNo());
        mv.addAttribute("list", showVos);

        return "view/balance";
    }


    @ResponseBody
    @PostMapping(value = "orderDataList")
    public JqGridVo orderDataList(SysTrade vo, Integer rows, Integer page) {
        Long id = getUserInfo().getId();
        vo.setAgentId(id);
        return orderService.queryOrderByUserId(vo, page, rows);
    }


    @ResponseBody
    @PostMapping(value = "orderBillList")
    public JqGridVo orderDataList(SysTradeSummary vo, Integer rows, Integer page) {
        Long id = getUserInfo().getId();
        vo.setAgentId(id);
        return sysTradeSummaryService.querydataByAgent(vo, page, rows);
    }


    @ResponseBody
    @RequestMapping(value = "exportExcle")
    public void exportExcle(SysTrade vo, HttpServletResponse response) throws Exception {
        Long id = getUserInfo().getId();
        vo.setAgentId(id);
        JqGridVo jqGridVo = orderService.queryOrderByUserId(vo, 1, 100000);
        List<?> rows1 = jqGridVo.getRows();

        ExportParams exportParams = new ExportParams("订单列表", "订单列表 ", ExcelType.XSSF);

        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, SysTrade.class, rows1);

        ServletOutputStream outputStream = response.getOutputStream();

        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment; filename="
                + URLEncoder.encode(getUserInfo().getMerchMerchantNo() + "_" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + ".xlsx", "UTF-8"));

        sheets.write(outputStream);
        outputStream.close();

        sheets.close();

    }


    private SysMerchant getUserInfo() {
        return (SysMerchant) httpSession.getAttribute("login");
    }

}
